package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 护理等级Service业务层处理
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /*redis缓存*/
    @Autowired
    private RedisTemplate redisTemplate;
    //缓存key前缀
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */

    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Cacheable(cacheNames = "nursingLevel", key = "'all'")
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int insertNursingLevel(NursingLevel nursingLevel) {
        nursingLevel.setCreateTime(DateUtils.getNowDate());
        return nursingLevelMapper.insert(nursingLevel);
    }

    /*删除缓存方法*/
    private void deteleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int flag = nursingLevelMapper.updateById(nursingLevel);
        deteleteCache();
        return flag;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int deleteNursingLevelByIds(Integer[] ids) {
        int flag = nursingLevelMapper.deleteBatchIds(List.of(ids));
        deteleteCache();
        return flag;
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int deleteNursingLevelById(Integer id) {
        int flag = nursingLevelMapper.deleteById(id);
        deteleteCache();
        return flag;
    }

    /**
     * 查询所有的护理等级, 状态为启用
     *
     * @return
     */
    @Cacheable(cacheNames = "nursingLevel", key = "'all'")
    @Override
    public List<NursingLevel> listAll() {
/*        //数据类型选择时，要考虑到美观所以要排序，但是zset结构复杂不合适，list和String相比String更方便
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if (!CollectionUtils.isEmpty(list)) {
            log.info("缓存命中");
            return list;
        }*/
        //缓存中没有的话构建查询条件
        List<NursingLevel> list = lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        /*  redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
         */
        return list;
    }
}
